package cn.dankal.flyaudio.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.library.AutoFlowLayout;
import com.example.library.FlowAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewHolder;
import cn.dankal.basiclib.pojo.ProductAttrEntity;
import cn.dankal.basiclib.pojo.ProductDetailAttrEntity;
import cn.dankal.basiclib.pojo.ProductDetailItemEntity;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.pop.AddShopCarPop;
import cn.dankal.flyaudio.store.SearchActivity;

public class ProductAttrAdapter extends BaseRecyclerViewAdapter<ProductAttrEntity> {

    private Map<String, String> params = new HashMap<>();

    private String skuItemDesc = "";

    private List<ProductDetailItemEntity> attrEntity;

    private ProductDetailItemEntity curSku;

    private AddShopCarPop pop;

    public ProductAttrAdapter(List<ProductDetailItemEntity> list, AddShopCarPop addShopCarPop) {
        this.attrEntity = list;
        this.pop = addShopCarPop;
    }

    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.item_attr;
    }


    @Override
    protected BaseRecyclerViewHolder getViewHolder(ViewGroup parent, View rootView, int viewType) {
        return new ViewHolder(rootView);
    }

    public Map<String, String> getParams() {
        return params;
    }

    private void handlerParams() {
        if (attrEntity == null || attrEntity.size() == 0) {
            return;
        }
        int count = 0;
        for (ProductDetailItemEntity productDetailItemEntity : attrEntity) {
            List<ProductDetailAttrEntity> attrEntities = productDetailItemEntity.getAttrList();
            if (attrEntities == null || attrEntities.isEmpty()) {
                return;
            }
            for (ProductDetailAttrEntity attr : attrEntities) {
                if (TextUtils.equals(params.get(attr.getAttrKey()), attr.getAttrValue())) {
                    //参数匹配
                    count++;
                }
            }
            if (count == params.size()) {
                //确认此参数
                return;
            } else {
                count = 0;
            }
        }
    }

    private ProductDetailItemEntity check() {
        if (attrEntity == null || attrEntity.size() == 0) {
            return null;
        }
        int count = 0;
        for (ProductDetailItemEntity productDetailItemEntity : attrEntity) {
            List<ProductDetailAttrEntity> attrEntities = productDetailItemEntity.getAttrList();
            if (attrEntities == null || attrEntities.isEmpty()) {
                return null;
            }
            for (ProductDetailAttrEntity attr : attrEntities) {
                if (TextUtils.equals(params.get(attr.getAttrKey()), attr.getAttrValue())) {
                    //参数匹配
                    count++;
                }
            }
            if (count == params.size()) {
                //确认此参数
                return productDetailItemEntity;
            } else {
                count = 0;
            }
        }
        return null;
    }


    public class ViewHolder extends BaseRecyclerViewHolder<ProductAttrEntity> {

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void onBindData(ProductAttrEntity data, int position) {
            setText(R.id.tv_attr_name, data.getAttrKey());
            AutoFlowLayout autoFlowLayout = itemView.findViewById(R.id.ll_flower_list);
//            autoFlowLayout.setSingleLine(true);
            autoFlowLayout.setAdapter(new FlowAdapter(data.getValueSet()) {
                @Override
                public View getView(int i) {
                    View rootView = LayoutInflater.from(getContext()).inflate(R.layout.item_attrs_text, null, false);
                    TextView auto_tv = rootView.findViewById(R.id.tv_text);
                    auto_tv.setText((data.getValueSet().get(i)));
                    return rootView;
                }
            });
            autoFlowLayout.setVerticalSpace(10);
            autoFlowLayout.setOnItemClickListener(new AutoFlowLayout.OnItemClickListener() {
                @Override
                public void onItemClick(int i, View view) {
                    TextView tv = view.findViewById(R.id.tv_text);
                    if(view.isSelected()){
                        params.put(data.getAttrKey(), tv.getText().toString());
                    }else {
                        params.remove(data.getAttrKey());
                    }
                    if (attrEntity != null && attrEntity.size() > 1) {
                        if (params.size() == getDatas().size()) {
                            ProductDetailItemEntity itemEntity = check();
                            ProductAttrAdapter.this.pop.setCurrentSku(itemEntity);
                        }
                    }
                }
            });
        }
    }
}
